package cn.com.recode.b2找出泥团函数提取;

import java.util.Enumeration;
import java.util.Vector;

public class Customer {

    private String _name;

    private Vector _rentals = new Vector();

    public Customer(String name) {
        this._name = name;
    }

    public void addRental(Rental rental) {
        _rentals.add(rental);
    }

    public String getName() {
        return _name;
    }

    /**
     * 1.找出代码的泥团, switch语句
     * 2. 任何不会被修改的变量可以被当成参数传入新的函数，会被修改的变量需要格外小心
     * 3.变量名称规范化，可通过名字即可知道用意
     *
     * @return
     */
    public String statement() {
        double totalAmout = 0;
        int frenquentRenterPoints = 0;
        Enumeration rentals = _rentals.elements();
        String result = "Rental  Record for " + getName() + "\n";
        while (rentals.hasMoreElements()) {
            double thisAmout = 0;
            Rental each = (Rental) rentals.nextElement();
            thisAmout = each.getCharge();
            frenquentRenterPoints++;
            if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && each.getDaysRented() > 1) {
                frenquentRenterPoints++;
            }

            result += "\t" + each.getMovie().getTitle() + "\t" +
                    String.valueOf(thisAmout) + "\n";

            totalAmout += thisAmout;
        }

        result += "Amout owed is " + String.valueOf(totalAmout) + "\n";
        result += "You earned " + String.valueOf(frenquentRenterPoints) + " frequent renter points";
        return result;
    }



}